package com.tbit.uqbike.config.mvc;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.tbit.uqbike.client.controller.interceptor.LanguageInterceptor;
import com.tbit.uqbike.client.controller.interceptor.LoginInterceptor;
import com.tbit.uqbike.client.exception.MyAcceptHeaderLocaleResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzhuang
 * @Description:
 * @date 2022/7/29 17:45
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    private CommonHeaderResolver commonHeaderResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
        resolvers.add(commonHeaderResolver);
    }

    /**
     * 注册自定义的LocaleResolver
     **/
    @Bean
    public LocaleResolver localeResolver() {
        MyAcceptHeaderLocaleResolver myAcceptHeaderLocaleResolver = new MyAcceptHeaderLocaleResolver();
        return myAcceptHeaderLocaleResolver;
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat);
//        // Date格式设置
//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        //3、在convert中添加配置信息
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.valueOf("text/html;charset=UTF-8"));
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);
    }


    @Bean
    public LanguageInterceptor languageInterceptor() {
        return new LanguageInterceptor();
    }


    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 添加拦截器
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(languageInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/heartbeat.do");

        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/*/*")
                .excludePathPatterns("/user/login.do")
                .excludePathPatterns("/user/wxLogin.do")
                .excludePathPatterns("/user/heartbeat.do")
                .excludePathPatterns("/user/checkToken.do")
                .excludePathPatterns("/sms/sendAuthCode.do")
                .excludePathPatterns("/sms/v2/sendAuthCode.do")
                .excludePathPatterns("/sms/needCaptcha.do")
                .excludePathPatterns("/weixinPay/notify.do")
                .excludePathPatterns("/machine/returnBike.do")
                .excludePathPatterns("/parkPoint/getNear.do")
                .excludePathPatterns("/parkPoint/getNearPark.do")
                .excludePathPatterns("/machineStatus/getNearMachine.do")
                .excludePathPatterns("/parkPoint/getByParkPointId.do")
                .excludePathPatterns("/calibrate/commonOriginal.do")
                .excludePathPatterns("/zhifubaoPay/alipayNotify.do")
                .excludePathPatterns("/parkPointImage/getByParkPointId.do")
                .excludePathPatterns("/parkPointImage/getByImageId.do")
                .excludePathPatterns("/systemMsg/getNew.do")
                .excludePathPatterns("/systemMsg/getByLocation.do")
                .excludePathPatterns("/adAccount/getByLocation.do")
                .excludePathPatterns("/debug/*.do")
                .excludePathPatterns("/geo/find.do")
                .excludePathPatterns("/geo/getInGeo.do")
                .excludePathPatterns("/map/getAddress.do")
                .excludePathPatterns("/mpBrand/getByAccountId.do")
                .excludePathPatterns("/image/getByImageId.do")
                .excludePathPatterns("/payPal/callBack.do")
                .excludePathPatterns("/suggestion/add.do")
                .excludePathPatterns("/machine/getFee.do")
                .excludePathPatterns("/account/getPhone.do")
                .excludePathPatterns("/machine/getExpectFee.do")
                .excludePathPatterns("/adAccountDeposit/getByLocation.do")
                .excludePathPatterns("/payPoint/callbackInform.do")
                .excludePathPatterns("/prohibitArea/getByArea.do")
                .excludePathPatterns("/user/zfbLogin.do")
                .excludePathPatterns("/machine/checkNewParkVoice.do")
                .excludePathPatterns("/user/oneLogin.do")
                .excludePathPatterns("/debug/getByType.do")
                .excludePathPatterns("/machine/addBattery.do")
                .excludePathPatterns("/machine/openApp.do")
                .excludePathPatterns("/adapay/callback.do")
                .excludePathPatterns("/account/getConfig.do")
                .excludePathPatterns("/adAccount/getByAccountId.do")
                .excludePathPatterns("/user/returnMoneyApi.do")
                .excludePathPatterns("/user/returnMoneyApiNew.do")
                .excludePathPatterns("/weixinPay/getAccessToken.do")
                .excludePathPatterns("/weixinPay/sendLockMsg.do")
                .excludePathPatterns("/weixinPay/getUrlScheme.do")
                .excludePathPatterns("/menu/query.do")
                .excludePathPatterns("/clientactuator/*")
                .excludePathPatterns("/payPoint/outTime.do")
                .excludePathPatterns("/version.do")
                .excludePathPatterns("/parkPoint/getVert.do")
                .excludePathPatterns("/parkPoint/refresh.do")
                .excludePathPatterns("/busDes/getByAccountId.do")
                .excludePathPatterns("/rideCard/queryPage.do")
                .excludePathPatterns("/memberFee/findMemberFee.do")
                .excludePathPatterns("/vipCard/queryPage.do")
                .excludePathPatterns("/couponMeal/getByAccountId.do")
                .excludePathPatterns("/returnDeposit/applyApi.do")
                .excludePathPatterns("/forbidden/getByAccountId.do")
                .excludePathPatterns("/machine/finishOrder.do")
                .excludePathPatterns("/user/otherLogin.do")
                .excludePathPatterns("/user/checkLogin.do")
                .excludePathPatterns("/user/getH5Sign.do")
                .excludePathPatterns("/user/H5Login.do")
                .excludePathPatterns("/lease/getLicensePrefix.do")
                .excludePathPatterns("/account/getAccountConfigExt.do")
                .excludePathPatterns("/terControl/rpc/tempPower.do")
                .excludePathPatterns("/accountSpike/rpc/checkTempPower.do")
                .excludePathPatterns("/event/applyTraceId.do")
                .excludePathPatterns("/wx/pushNotReturnBikeMsg.do")
                .excludePathPatterns("/captcha/*.do")
                .excludePathPatterns("/outAge/getOutAge.do")
                .excludePathPatterns("/invoiceRecord/reAutoInvoicing.do")
                .excludePathPatterns("/account/getPhoneLogin.do")
                .excludePathPatterns("/invoiceRecord/invoiceNotify.do")
        ;

    }


    /**
     * 严格按照*。do匹配 会影响‘项目内静态资源’的访问
     */
    /*@Bean(name = "dispatcherServletRegistration")
    public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
                                                                           WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
        DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
                "*.do");
        registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
        registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
        multipartConfig.ifAvailable(registration::setMultipartConfig);
        return registration;
    }*/
}
